Cyclomatic Complexity (CC)

Description:

CC represents number of cycles in the measured method. This measure represents the cognitive complexity of the class. It counts the number of possible paths through an algorithm by counting the number of distinct regions on a flowgraph, meaning the number of if, for, and while statements in the operation's body. Case labels for switch statements are counted if the Case as branch property is activated.

A strict definition of CC (introduced by McCabe in 1976) looks at a program's control flow graph as a measure of its complexity:

CC = L - N + 2P

where L is the number of links in the control flow graph, N is the number of nodes in the control flow graph, and P is the number of disconnected parts in the control flow graph.

For example, consider a method which consists of an if statement:

   if (x > 0) { 
       x++; 
   } else { 
       x--; 
   } 

CC = L - N + 2P = 4 - 4 + 2*1 = 2

A less formal definition is:

CC = D + 1

where D is the number of binary decisions in the control flow graph, if it has only one entry and exit. In other words, the number of if, for and while statements and number of logical and and or operators.

For the example above: CC = D + 1 = 1 + 1 = 2